package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.doublefish.doublefishrobot.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:dugaofeng on 2024/10/23 16:01
 */
public class ModeHeaderIndexView extends LinearLayout {

    private Context mContext;
    private RecyclerView recyclerView;
    private ImageView addIv;
    private IndexAdapter indexAdapter;

    private int size;    //size
    private int currentIndex = 0;  //当前index
    private boolean editAble = false;  //是否可编辑
    private List<IndexAdapter.ModeHeaderIndexBean> list = new ArrayList<>();
    private OnModeHeaderIndexViewListner onModeHeaderIndexViewListner;

    public ModeHeaderIndexView(@NonNull Context context) {
        this(context, null);
    }

    public ModeHeaderIndexView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeHeaderIndexView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
        initListner();
    }

    public void setOnModeHeaderIndexViewListner(OnModeHeaderIndexViewListner onModeHeaderIndexViewListner) {
        this.onModeHeaderIndexViewListner = onModeHeaderIndexViewListner;
    }

    public void setEditAble(boolean editAble) {
        this.editAble = editAble;
        addIv.setVisibility(editAble ? VISIBLE : GONE);
        indexAdapter.setShowDeleteBtn(editAble);
	    indexAdapter.notifyDataSetChanged();
    }

    public void setTotalSize(int size) {
        this.size = size;

        while (list.size() < size) {
            IndexAdapter.ModeHeaderIndexBean indexBean = new IndexAdapter.ModeHeaderIndexBean(list.size());
            list.add(indexBean);
        }
        while (list.size() > size) {
            list.remove(list.size() - 1);
        }

        indexAdapter.notifyDataSetChanged();
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
        indexAdapter.setCurrentIndex(currentIndex);
        indexAdapter.notifyDataSetChanged();

        recyclerView.smoothScrollToPosition(currentIndex);
    }

    private void initView() {
        inflate(getContext(), R.layout.view_mode_custom_header_index, this);

        recyclerView = findViewById(R.id.recycler);
        addIv = findViewById(R.id.iv_add);
        addIv.setVisibility(GONE);
    }

    private void initListner() {
        indexAdapter = new IndexAdapter(list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(indexAdapter);

        indexAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                setCurrentIndex(position);
                if (onModeHeaderIndexViewListner != null) {
                    onModeHeaderIndexViewListner.onIndexChanged(position);
                }
            }
        });

        addIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onModeHeaderIndexViewListner != null) {
                    onModeHeaderIndexViewListner.onAddStep();
                }
            }
        });
    }

    //region  mark: adapter
    static class IndexAdapter extends BaseQuickAdapter<IndexAdapter.ModeHeaderIndexBean, IndexAdapter.ViewHolder> {
        private int currentIndex = 0;  //当前index
        private boolean isShowDeleteBtn = false; // 是否显示删除按钮

        public IndexAdapter(List<ModeHeaderIndexBean> data) {
            super(R.layout.item_mode_step_index, data);
        }

        @Override
        protected void convert(IndexAdapter.ViewHolder helper, ModeHeaderIndexBean item) {
            helper.getIndexTv().setText(item.getText());
            helper.getIndexTv().setSelected(item.getIndex() == currentIndex);
            helper.setShowDeleteBtn(isShowDeleteBtn);
        }

        public void setShowDeleteBtn(boolean showDeleteBtn) {
            isShowDeleteBtn = showDeleteBtn;
        }

        public void setCurrentIndex(int currentIndex) {
            this.currentIndex = currentIndex;
        }

        class ViewHolder extends BaseViewHolder {

            private TextView indexTv;
            private ImageView deleteIv;

            private boolean isShowDeleteBtn = false; // 是否显示删除按钮

            public ViewHolder(View view) {
                super(view);
                indexTv = view.findViewById(R.id.tv_index);
                deleteIv = view.findViewById(R.id.iv_delete);
            }

            public TextView getIndexTv() {
                return indexTv;
            }

            public void setShowDeleteBtn(boolean showDeleteBtn) {
                isShowDeleteBtn = showDeleteBtn;

                deleteIv.setVisibility(showDeleteBtn ? VISIBLE : INVISIBLE);
            }
        }

        static class ModeHeaderIndexBean {
            private int index;
            private String text;

            public ModeHeaderIndexBean(int index) {
                this.index = index;
                this.text = "" + (index + 1);
            }

            public int getIndex() {
                return index;
            }

            public String getText() {
                return text;
            }
        }
    }


    //endregion

    //region  mark: OnModeHeaderIndexViewListner

    public interface OnModeHeaderIndexViewListner {
        void onIndexChanged(int index);
        void onAddStep();
    }

    //endregion

}
